<?php
abstract class AbstractPlugins {
	public function assign($name, $value = '') {
		$view = Think::instance ( 'View' );
		$view->assign ( $name, $value );
	}
	public function fetch($templateFile = '', $charset = '', $contentType = 'text/html', $type) {
		$dirName = getcwd () . '/Plugins/' . $type . DIRECTORY_SEPARATOR . "html" . DIRECTORY_SEPARATOR . $templateFile . C ( 'TMPL_TEMPLATE_SUFFIX' );
		$view = Think::instance ( 'View' );
		return $view->fetch ( $dirName );
	}
	public function display($templateFile = '', $type, $charset = '', $contentType = 'text/html') {
		echo $this->fetch ( $templateFile, $charset, $contentType, $type );
	}
	
	public function error($message, $jumpUrl = '') {
		$this->dispatchJump ( $message, 0, $jumpUrl );
	}

	public function success($message, $jumpUrl = '') {
		$this->dispatchJump ( $message, 1, $jumpUrl );
	}
	
	private function dispatchJump($message, $status = 1, $jumpUrl = '') {
		$view = Think::instance ( 'View' );
		if (! empty ( $jumpUrl ))
			$this->assign ( 'jumpUrl', $jumpUrl );
			// 提示标题
		$this->assign ( 'msgTitle', $status ? L ( '_OPERATION_SUCCESS_' ) : L ( '_OPERATION_FAIL_' ) );
		// 如果设置了关闭窗口，则提示完毕后自动关闭窗口
		if ($view->get ( 'closeWin' ))
			$this->assign ( 'jumpUrl', 'javascript:window.close();' );
		$this->assign ( 'status', $status ); // 状态
		                                     // 保证输出不受静态缓存影响
		C ( 'HTML_CACHE_ON', false );
		if ($status) { // 发送成功信息
			$this->assign ( 'message', $message ); // 提示信息
			                                       // 成功操作后默认停留1秒
			if (! $view->get ( 'waitSecond' ))
				$this->assign ( 'waitSecond', '1' );
				// 默认操作成功自动返回操作前页面
			if (! $view->get ( 'jumpUrl' ))
				$this->assign ( "jumpUrl", $_SERVER ["HTTP_REFERER"] );
			$view->display ( C ( 'TMPL_ACTION_SUCCESS' ) );
		} else {
			$this->assign ( 'error', $message ); // 提示信息
			                                     // 发生错误时候默认停留3秒
			if (! $view->get ( 'waitSecond' ))
				$this->assign ( 'waitSecond', '3' );
				// 默认发生错误的话自动返回上页
			if (! $view->get ( 'jumpUrl' ))
				$this->assign ( 'jumpUrl', "javascript:history.back(-1);" );
			$view->display ( C ( 'TMPL_ACTION_ERROR' ) );
			// 中止执行 避免出错后继续执行
			exit ();
		}
	}
}
